Bit manipulation is a technique in competitive programming that involves the manipulation of individual bits in binary representations of numbers. It is a valuable technique in competitive programming because it allows you to solve problems efficiently, often reducing time complexity and memory usage....
Given L and R. The task is to find the number in whose binary representation all bits between the L-th and R-th index are set and the rest of the bits are unset. The binary representation is 32 bits. Examples:...
Given an integer array arr[] of length N consisting of positive integers, the task is to minimize the number of steps required to reach the index ‘N-1’. At a given step if we are at index ‘i’ we can go to index ‘i-arr[i]’ or ‘i+arr[i]’ given we have not visited those indexes before. Also, we cannot go outside the bounds of the array. Print -1 if there is not possible way.Examples:...
Given two integers N and K, the task is to find whether it is possible to represent N as the sum of exactly K powers of 2. If possible, then print K positive integers such that they are powers of 2 and their sum is exactly equal to N. Otherwise, print “Impossible”. If multiple answers exist, print any....
Given a number N, there are two steps to be performed....
Given two integers N and K, the task is to find the smallest number greater than N whose Kth bit in its binary representation is set....
Given an array of positive elements arr[] of size N. You can select any index i such that i + 1 < N and swap arr[i], arr[i + 1] if they have the same number of set bits in their binary representation. The task is to determine whether it is possible to sort the array in ascending order by swapping any number of times. If it is possible, you should return true, otherwise return false....
Given two 32-bit numbers, N and M, and two-bit positions, i and j. Write a method to insert M into N such that M starts at bit j and ends at bit i. You can assume that the bits j through i have enough space to fit all of M. Assuming index start from 0.Examples:...
Given an integer N, the task is to find the smallest integer X such that it has no odd position set and X ? N. Note: The positioning of bits is assumed from the right side and the first bit is assumed to be the 0th bit....
Given an integer N, the task is to print the number obtained by unsetting the least significant K bits from N....
Given two numbers a and b. The task is to subtract b from a by using 2’s Complement method.Note: Negative numbers represented as 2’s Complement of Positive Numbers.For example, -5 can be represented in binary form as 2’s Complement of 5. Look at the image below:...
Given an array arr[] of N pairs, where each array element denotes a query of the form {L, R}, the task is to find the count of numbers in the range [L, R], having only 3-set bits for each query {L, R}....